﻿// Licensed under GNU GPL V2.
// http://sps2010listmanager.codeplex.com/
// (c) 2010 by Daniel Bedarf

using System;
using System.Collections.Generic;
using System.Windows;
using Extensions;

namespace CodePlex.DBedarf.Sharepoint.ListManager
{
    /// <summary>
    /// Interaktionslogik für ListEventhandler.xaml
    /// </summary>
    public partial class ListPropertyPage : AvalonDock.DocumentContent
    {

        //TODO R2: connect all chkBoxes to _bSomeThingChanged

        Microsoft.SharePoint.SPList _list;
        bool _bSomeThingChanged;

        /// <summary>
        /// used to invoke a refresh of content in othe applicationparts
        /// </summary>
        RoutedEventHandler refreshLists;
        logJournalMessageDelegate log;


        /// <summary>
        /// Initializes a new instance of the <see cref="ListPropertyPage"/> class.
        /// </summary>
        /// <param name="refreshListsMethod">The refresh lists method.</param>
        /// <param name="logMethod">The log method.</param>
        public ListPropertyPage(RoutedEventHandler refreshListsMethod, logJournalMessageDelegate logMethod)
        {
            InitializeComponent();

            refreshLists = refreshListsMethod;
            log = logMethod;
            _bSomeThingChanged = false;
        }


        /// <summary>
        /// Sets the list to edit.
        /// </summary>
        /// <value>The list</value>
        public Microsoft.SharePoint.SPList Lists
        {
            set
            {
                if (_bSomeThingChanged) if (WindowExtender.AskYesNo("Changes detected", "There are unsaved changes to the list.\n\nDo you want to drop that changes?") == false) return;

                _list = value;
                if (_list != null)
                {
                    stpEdit.IsEnabled = true;

                }
                else stpEdit.IsEnabled = false;

                Action_GetListValues();
            }
        }

        /// <summary>
        /// read all list props and set it to the ui
        /// </summary>
        private void Action_GetListValues()
        {

            xCaml.Visibility = Visibility.Visible;
            xName.Visibility = Visibility.Visible;


            txtColumns.Text = "";
            //Buttons entsprechend der Fields vorbelegen
            txtColumns.Text += _list.Title;

            //TODO per Binding lösen
            //chkShowNewForm.IsChecked = field.ShowInNewForm;
            //chkShowEditForm.IsChecked = field.ShowInEditForm;
            //chkShowViewForm.IsChecked = field.ShowInViewForms;
            //chkShowDisplayForm.IsChecked = field.ShowInDisplayForm;
            //chkShowSettings.IsChecked = field.ShowInListSettings;
            //TODO P1: ansichten etc
            //chkIsRequiered.IsChecked = field.Required;

            txtSchema.Text = _list.SchemaXml;
            //txtStaticName.Text = field.StaticName;
            _bSomeThingChanged = false;
        }

        /// <summary>
        /// Handles the SaveSettings event of the Useraction control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Useraction_SaveSettings(object sender, RoutedEventArgs e)
        {

            //foreach (Microsoft.SharePoint.SPField field in _fields)
            //{
            //    try
            //    {

            //        string msg = "";
            //        bool needUpdate = false;
            //        if (chkShowNewForm.IsChecked != field.ShowInNewForm)
            //        {
            //            needUpdate = true;
            //            field.ShowInNewForm = chkShowNewForm.IsChecked;
            //            msg += "ShowInNewForm: " + chkShowNewForm.IsChecked.ToString() + ", ";
            //        }

            //        if (chkShowEditForm.IsChecked != field.ShowInEditForm)
            //        {
            //            needUpdate = true;
            //            field.ShowInEditForm = chkShowEditForm.IsChecked;
            //            msg += "ShowInEditForm: " + chkShowEditForm.IsChecked.ToString() + ", ";
            //        }

            //        if (chkShowViewForm.IsChecked != field.ShowInViewForms)
            //        {
            //            needUpdate = true;
            //            field.ShowInViewForms = chkShowViewForm.IsChecked;
            //            msg += "ShowInViewForms: " + chkShowViewForm.IsChecked.ToString() + ", ";
            //        }

            //        if (chkShowDisplayForm.IsChecked != field.ShowInDisplayForm)
            //        {
            //            needUpdate = true;
            //            field.ShowInDisplayForm = chkShowDisplayForm.IsChecked;
            //            msg += "ShowInDisplayForm: " + chkShowDisplayForm.IsChecked.ToString() + ", ";
            //        }

            //        if (chkShowSettings.IsChecked != field.ShowInListSettings)
            //        {
            //            needUpdate = true;
            //            field.ShowInListSettings = chkShowSettings.IsChecked;
            //            msg += "ShowInListSettings: " + chkShowSettings.IsChecked.ToString() + ", ";
            //        }

            //        //Access
            //        if (chkIsRequiered.IsChecked != field.Required && chkIsRequiered.IsChecked != null)
            //        {
            //            needUpdate = true;
            //            field.Required = (bool)chkIsRequiered.IsChecked;
            //            msg += "Required: " + chkIsRequiered.IsChecked.ToString() + ", ";
            //        }

            //        if (chkCanDeleted.IsChecked != field.AllowDeletion && chkCanDeleted.IsChecked != null)
            //        {
            //            needUpdate = true;
            //            field.AllowDeletion = chkCanDeleted.IsChecked;
            //            msg += "AllowDeletion: " + chkCanDeleted.IsChecked.ToString() + ", ";
            //        }

            //        if (chkReadOnly.IsChecked != field.ReadOnlyField && chkReadOnly.IsChecked != null)
            //        {
            //            needUpdate = true;
            //            field.ReadOnlyField = (bool)chkReadOnly.IsChecked;
            //            msg += "ReadOnly: " + chkReadOnly.IsChecked.ToString() + ", ";
            //        }

            //        if (chkHidden.IsChecked != field.Hidden && chkHidden.IsChecked != null)
            //        {
            //            needUpdate = true;
            //            field.Hidden = (bool)chkHidden.IsChecked;
            //            msg += "Hidden: " + chkHidden.IsChecked.ToString() + ", ";
            //        }

            //        if (chkNoCrawlBySearchEngine.IsChecked != field.NoCrawl && chkNoCrawlBySearchEngine.IsChecked != null)
            //        {
            //            needUpdate = true;
            //            field.NoCrawl = (bool)chkNoCrawlBySearchEngine.IsChecked;
            //            msg += "NoCrawl: " + chkNoCrawlBySearchEngine.IsChecked.ToString() + ", ";
            //        }
            //        if (_fields.Count == 1)
            //        {
            //            if (field.StaticName.Equals(txtStaticName.Text) == false)
            //            {
            //                needUpdate = true;
            //                msg += string.Format("change StaticName from \"{0}\" to \"{1}\"", field.StaticName, txtStaticName.Text);
            //                field.StaticName = txtStaticName.Text;
            //            }
            //        }

            //        if (needUpdate)
            //        {
            //            field.Update();
            //            if (log != null) log(false, "change column \"{0}\" set: {1}", field.Title, msg);
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        if (log != null)
            //        {
            //            log(true, "Error while set propertys on column \"{0}\". Message: {1}", field.Title, ex.Message);
            //        }
            //        else
            //        {
            //            //alternate
            //            HandleError(ex);
            //        }
            //    }

            //}//end for each field
            //_bSomeThingChanged = false;
            //TODO R3 - Selection geht verloren bei RefreshFields, evtl. verhindern...
            //if (refreshFields != null) refreshFields(sender, e);
            throw new NotImplementedException();
            //TODO P1 Impl.
        }

        /// <summary>
        /// Handles the Refresh event of the Useraction control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Useraction_Refresh(object sender, RoutedEventArgs e)
        {
            try
            {
                Action_GetListValues();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        /// <summary>
        /// Handles the error.
        /// </summary>
        /// <param name="ex">The ex.</param>
        private void HandleError(Exception ex)
        {
            WindowExtender.ShowError(ex);
        }
    }
}